Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added OAuth Support for Public APIs with TokenManager Integration #813

Merged
merged 15 commits into from
Oct 3, 2024

Conversation

sbansla
Copy link
Contributor

@sbansla sbansla commented Sep 18, 2024

Feature

Added OAuth functionality for public APIs.

  • We are introducing an OAuth feature where clients can initialize credentials using ClientCredentialProvider and then call an API. The API will be called using a token generated from the client credentials passed via ClientCredentialProvider.
  • Token retrieval will occur without client intervention.
  • Created a TokenManager service to fetch tokens using the TokenCreator API from the preview-iam domain.
  • Updated documentation and examples in the README."

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the main branch
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation about the functionality in the appropriate .md file
  • I have added inline documentation to the code I modified

If you have questions, please file a support ticket, or create a GitHub Issue in this repository.

@sbansla sbansla changed the title Public oauth rest client chore: Public oauth rest client Sep 18, 2024
@sbansla sbansla changed the base branch from OAuth-Public-APIs to main September 19, 2024 15:56
@sbansla sbansla changed the title chore: Public oauth rest client chore: Public oauth rest client [DO-NOT-MERGE] Sep 19, 2024
@sbansla sbansla changed the title chore: Public oauth rest client [DO-NOT-MERGE] feat: Public oauth rest client [DO-NOT-MERGE] Sep 27, 2024
@sbansla sbansla changed the title feat: Public oauth rest client [DO-NOT-MERGE] feat: Public OAuth [DO-NOT-MERGE] Sep 27, 2024
@sbansla sbansla changed the title feat: Public OAuth [DO-NOT-MERGE] feat: Added OAuth Support for Public APIs with TokenManager Integration [DO-NOT-MERGE] Sep 27, 2024
@sbansla sbansla marked this pull request as ready for review September 30, 2024 12:30
@sbansla sbansla changed the title feat: Added OAuth Support for Public APIs with TokenManager Integration [DO-NOT-MERGE] feat: Added OAuth Support for Public APIs with TokenManager Integration Oct 3, 2024
@@ -214,6 +214,12 @@ public class Example {
}
```

### OAuth Feature
We are introducing Client Credentials Flow-based OAuth 2.0 authentication.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we mention the orgs auth is also in beta? since we changed back from preview?

@@ -1,12 +0,0 @@
package com.twilio.annotations;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous idea was to move our API from preview to beta and public as backend API moves, but we can not track that, so to keep simple we will have only Beta


@Override
public String getAuthString() {
return "Bearer " + token;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a null check here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call fetch token

final String credentials = this.username + ":" + this.password;
final String encoded = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.US_ASCII));
return "Basic " + encoded;
if (username != null && password != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we convert username and password into basic auth strategy in any of the previous steps and just follow auth strategy based methods here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good suggestion, I think we might have to check if it will break existing customer who are using below configuration:
https://github.com/twilio/twilio-java/blob/main/advanced-examples/custom-http-client.md

@@ -59,7 +68,14 @@ protected TwilioRestClient(Builder b) {
* @return Response object
*/
public Response request(final Request request) {
request.setAuth(username, password);
if (username != null && password != null) {
request.setAuth(username, password);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move get auth string to individual auth strategy class and just fetch and set the value in request class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case if we moved username and password to authstrategy class, we can do that.

if(response != null) {
int statusCode = response.getStatusCode();
if (statusCode == HTTP_STATUS_CODE_UNAUTHORIZED && authStrategy != null && EnumConstants.AuthType.TOKEN.equals(authStrategy.getAuthType())) {
((TokenAuthStrategy)authStrategy).fetchToken();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving fetchtoken logic inside auth strategy class and just fetching the auth string would be more aligned to the concept of abstraction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed over call, this code is required for fetching token in case there is 401
Will be adding fetchToken in getAuthString() method as mentioned in previous comment.

Copy link
Contributor

@AsabuHere AsabuHere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

sonarcloud bot commented Oct 3, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
44.0% Coverage on New Code (required ≥ 80%)
B Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@sbansla sbansla merged commit 30e91a6 into main Oct 3, 2024
11 of 12 checks passed
@sbansla sbansla deleted the public-oauth-rest-client branch October 3, 2024 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants